package org.osami.gateway.actuator.internal.swing;

import javax.swing.*;
import java.io.IOException;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;
import org.osami.gateway.sensor.data.SenseNode;
import org.osami.gateway.watteco.interfaces.ISwitch;
import org.osami.gateway.watteco.interfaces.IPowerMeter;
import org.osgi.service.device.Constants;


@Component
@Provides
public class SwingMockActuator implements ISwitch, IPowerMeter
{
    public final static int MATCH_SERIAL = 10;
    public final static int MATCH_TYPE = 5;

    public final static String POWER_KEY = "Power";

    @ServiceProperty(name = "ActuatorType", value = "")
    private String actuatorType;

    @ServiceProperty(name = "PowerConsumptionTurnedOff", value = "0")
    private Integer powerConsumptionTurnedOff;

    @ServiceProperty(name = "PowerConsumptionTurnedOn", value = "0")
    private Integer powerConsumptionTurnedOn;

    private boolean state;

    @ServiceProperty(name = Constants.DEVICE_CATEGORY,
                     value = "{Watteco}")
    private String[] categories;

    @ServiceProperty(name = Constants.DEVICE_SERIAL)
    private String serial;

    @ServiceProperty(name = "device.type",
                     value = "wattecosp")
    private String type;

    @ServiceProperty(value = SenseNode.TOPIC_PREFIX + "wattecosp")
    private String topic;

    private ActuatorFrame frame;

    @Validate
    public void start() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame = new ActuatorFrame(actuatorType, SwingMockActuator.this);
                frame.setTitle("Actuator UI: " + actuatorType);
                frame.setSerial(actuatorType);
                frame.setSize(350, 150);
                frame.setVisible(true);
            }
        });
    }

    @Invalidate
    public void stop() {
        frame.setVisible(false);
        frame.dispose();
    }


    public void noDriverFound() {
        System.out.println("SwingMockActuator.noDriverFound()");
    }

    public void switchOn() throws IOException
    {
        frame.switchOn();
        frame.setText("Power consumption: " + powerConsumptionTurnedOn.toString() + "W");
        state = true;
    }

    public void switchOff() throws IOException
    {
        frame.switchOff();
        frame.setText("Power consumption: " + powerConsumptionTurnedOff.toString() + "W");
        state = false;
    }

    public void toggleSwitch() throws IOException
    {
        state = !state;
        setSwitchState(state);
    }

    public int getSwitchState() throws IOException
    {
        return state ? 1 : 0;
    }

    public void setSwitchState(boolean state) throws IOException
    {
        if (state) {
            switchOn();
        }
        else {
            switchOff();
        }
    }

    public void startMeasurements() throws IOException
    {
        
    }

    public int getMeasurement() throws IOException
    {
        int value = powerConsumptionTurnedOff;
        if (state) {
            value = powerConsumptionTurnedOn;
        }
        return value;
    }

    public String getID()
    {
        return serial;
    }
}
